﻿using BCCommon;
using BCCommon.Enums;
using BCData.HelpDocument.HelpDocumentation;
using BCDto.HelpDocument.HelpDocumentation;
using BCEntity.HelpDocument.HelpDocumentation;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCData.HelpDocument.HelpDocumentCategory;
using BCService.HelpDocument.HelpDocumentCategory;
using BCDto.Sim.Admin.Admin;

namespace BCService.HelpDocument.HelpDocumentation
{
    public class HelpDocumentationService : IHelpDocumentationService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IHelpDocumentationData helpDocumentationData;
        private readonly IHelpDocumentCategoryService helpDocumentCategoryService;
        private readonly string key = string.Format("{0}", EntityNameConst.HelpDocumentationEntity);

        public HelpDocumentationService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IHelpDocumentationData helpDocumentationData,
            IHelpDocumentCategoryService helpDocumentCategoryService)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.helpDocumentationData = helpDocumentationData;
            this.helpDocumentCategoryService = helpDocumentCategoryService;
        }

        #region 检查帮助是否存在
        public bool Exists(long helpDocumentationId)
        {
            return this.helpDocumentationData.Exists(helpDocumentationId).GetAwaiter().GetResult();
        }
        public bool Exists(string documentCode)
        {
            return this.helpDocumentationData.Exists(documentCode).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加帮助
        public HelpDocumentationDto Add(HelpDocumentationRequestDto requestDto,AdminDto adminDto)
        {
            if (!this.helpDocumentCategoryService.Exists(requestDto.HelpDocumentCategoryId))
            {
                throw new ArgumentException("此帮助文档分类不存在!");
            }
            if (this.helpDocumentationData.Exists(requestDto.DocumentCode).Result)
            {
                throw new ArgumentException("此帮助文档码已存在!");
            }
            var categoryDto = this.helpDocumentCategoryService.Get(requestDto.HelpDocumentCategoryId);
            var entity = requestDto.As<HelpDocumentationEntity>();
            entity.DocumentCategoryName = categoryDto.DocumentCategoryName;
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;
            
            var result = this.helpDocumentationData.Add(entity).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<HelpDocumentationDto>();
        }
        #endregion

        #region 修改帮助
        public HelpDocumentationDto Update(HelpDocumentationPutDto putDto, AdminDto adminDto)
        {
            if (!this.Exists(putDto.HelpDocumentationId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            if (!this.helpDocumentCategoryService.Exists(putDto.HelpDocumentCategoryId))
            {
                throw new ArgumentException("此帮助文档分类不存在!");
            }
            if (this.helpDocumentationData.Exists(putDto.DocumentCode).Result)
            {
                throw new ArgumentException("此帮助文档码已存在!");
            }
            var categoryDto = this.helpDocumentCategoryService.Get(putDto.HelpDocumentCategoryId);
            var entity = putDto.As<HelpDocumentationEntity>();
            entity.DocumentCategoryName = categoryDto.DocumentCategoryName;
            entity.EditAdminName = adminDto.FullName;
            entity.EditAdminId = adminDto.AdminId;
            var result = this.helpDocumentationData.Update(entity).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<HelpDocumentationDto>();
        }
        #endregion

        #region 删除帮助
        public bool Delete(long helpDocumentationId)
        {
            if (!this.Exists(helpDocumentationId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.helpDocumentationData.Delete(helpDocumentationId).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 获取帮助
        public HelpDocumentationDto Get(long helpDocumentationId)
        {
            string cacheKey = this.redisService.GetKey(key, helpDocumentationId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.helpDocumentationData.GetEntity(helpDocumentationId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<HelpDocumentationDto>();
        }
        public HelpDocumentationDto Get(string documentCode)
        {
            string cacheKey = this.redisService.GetKey(key, documentCode);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.helpDocumentationData.GetEntity(documentCode).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<HelpDocumentationDto>();
        }
        #endregion

        #region 获取帮助列表
        public IEnumerable<HelpDocumentationDto> GetList(long helpDocumentCategoryId)
        {
            string cacheKey = this.redisService.GetKey(key, helpDocumentCategoryId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return this.helpDocumentationData.GetList(helpDocumentCategoryId).GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<HelpDocumentationDto>>();
        }
        #endregion

        #region 获取帮助分页
        public PageableList<HelpDocumentationDto> Query(long? helpDocumentCategoryId, string searchKey, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, helpDocumentCategoryId, searchKey, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.helpDocumentationData.Query(helpDocumentCategoryId, searchKey, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<HelpDocumentationDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<HelpDocumentationDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
    }
}